var ScenePlay = function(game){
    s = {
        game: game,
    }

    // 初始化
    var ball = Ball(game)
    var paddle = Paddle(game)
    var bg = BackGround(game)

    myUtils.bricks = LoadLevels(game, 1)

    game.registerActions("KeyJ", ball.fire)
    game.registerActions("KeyA", paddle.moveLeft)
    game.registerActions("KeyD", paddle.moveRight)

    window.addEventListener("mousedown", function(event){
        x = event.offsetX
        y = event.offsetY
        if (ball.clickd(x, y)){
            myUtils.clicked = true
        }
    })
    window.addEventListener("mousemove", function(event){
        if (myUtils.clicked){
            ball.x = event.offsetX
            ball.y = event.offsetY
        }
    })
    window.addEventListener("mouseup", function(event){
        myUtils.clicked = false
    })    


    s.update = function(){
        if (myUtils.paused){
            return
        }

        ball.move()

        // 判断游戏结束
        if (ball.y > paddle.y+10){
            var end = SceneEnd(game)
            game.replaceScene(end)
        }

        // 判断反弹
        if (paddle.collide(ball)){
            ball.reboundFrom(paddle)
        }
        for (i = 0; i < myUtils.bricks.length; i ++){
            if (myUtils.bricks[i].collide(ball)) {
                ball.reboundFrom(myUtils.bricks[i])
                myUtils.bricks[i].kill()
            }
        }
    }
    s.draw = function(){
        game.drawbg(bg)
        game.drawImage(paddle)
        game.drawImage(ball)

        for (i = 0; i < myUtils.bricks.length; i ++){
            if (myUtils.bricks[i].alive){
                game.drawImage(myUtils.bricks[i])
            }
        }

        game.ctx.font = "12px 'Microsoft YaHei Mono'"
        game.ctx.fillText("分数：" + socre, 10,290)
    }

    return s
}